
package com.android.finaltracer.model;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * This class represents a position with latitude and longitude
 * 
 * @author zhuhy1
 */
public class PositionInfo extends DataFormatBasic {
    private static final String TAG_LATITUDE = "latitude:";
    private static final String TAG_LONGITUDE = "longitude:";
    private int latitude;
    private int longitude;

    /**
     * Constructor
     */
    public PositionInfo() {
        this(0, 0);
    }

    public PositionInfo(int latitude, int longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public void setPosition(int latitude, int longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public int getLatitude() {
        return this.latitude;
    }

    public void setLatitude(int latitude) {
        this.latitude = latitude;
    }

    public int getLongitude() {
        return this.longitude;
    }

    public void setLongitude(int longitude) {
        this.longitude = longitude;
    }

    /**
     * static method to create PositionInfo
     */
    public static PositionInfo makePosition(int latitude, int longitude) {
        return new PositionInfo(latitude, longitude);
    }

    public String toString() {
        return "[" + this.latitude + ", " + this.longitude + "]";
    }

    public String encode() {
        String codeStr = "";
        try {
            JSONObject codeJson = new JSONObject(makeHeader());
            codeJson.put(TAG_BODY, new JSONObject().put(TAG_LATITUDE, latitude)
                    .put(TAG_LONGITUDE, longitude));
            codeStr += codeJson.toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return codeStr;
    }

    public boolean decode(String str) {
        try {
            JSONObject json = new JSONObject(str);
            latitude = json.getInt(TAG_LATITUDE);
            longitude = json.getInt(TAG_LONGITUDE);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public int getType() {
        // TODO Auto-generated method stub
        return DataFormatBasic.TYPE_POSITION_INFO;
    }
}
